package controler.states;
import controler.A_State;
import controler.A_UserCommand;
import controler.userCommands.AddToSelection;
import controler.userCommands.ChangeElementProperty;
import controler.userCommands.ChangePhysicProperty;
import controler.userCommands.EditPhysicShape;
import controler.userCommands.RemoveFromSelection;
import controler.userCommands.SwitchState;
import model.LevelEditorModelEvent;
import model.ModelUpdateType;
import phx.Vector;
import view.events.LevelEditorViewKeyboardEvent;
import view.events.LevelEditorViewMouseEvent;
import view.events.LevelEditorViewOtherEvent;

/**
 * @author Damien Mabin
 */

class PhysicPointSimpleSelection extends A_State
{

	public function new() 
	{
		super();
		this.addEventListener(LevelEditorViewMouseEvent.EVENT, OnLevelEditorMouseEvent);
		this.addEventListener(LevelEditorViewKeyboardEvent.EVENT, OnLevelEditorKeyboardEvent);
		this.addEventListener(LevelEditorViewOtherEvent.EVENT, OnLevelEditorOtherEvent);
	}
	
	private function OnLevelEditorMouseEvent(_Evt:LevelEditorViewMouseEvent):Void
	{
		switch(_Evt.m_Type)
		{
			case MOUSE_DOWN :
			{
				switch(_Evt.m_TargetType)
				{
					case PHYSIC_POINT :
					{
						if (!A_State.s_Model.IsSelected(Vector, _Evt.m_Target))
						{
							var firstCommand:Bool = true;
							var selection : Array<Vector> =  A_State.s_Model.VUE_GetSelection(Vector).copy();
							while(selection.length > 0)
							{
								var cmd : A_UserCommand = new RemoveFromSelection(null, Vector, selection.pop());
								if (!firstCommand)
								{
									cmd.m_Stackable = true;
								}
								else
								{
									firstCommand = false;
								}
								A_State.s_Controller.DoUserCommand(cmd);
							}
							var cmd : A_UserCommand = new AddToSelection(null, Vector, _Evt.m_Target);
							if (!firstCommand)
							{
								cmd.m_Stackable = true;
							}
							else
							{
									firstCommand = false;
							}
							A_State.s_Controller.DoUserCommand(cmd);
						}
						else
						{
							A_State.s_Controller.DoUserCommand(new SwitchState(A_State.s_Controller.MOVE_SELECTION));
						}
					}
					case SCENE :
					{
						var firstCommand:Bool = true;
						var selection : Array<Vector> =  A_State.s_Model.VUE_GetSelection(Vector).copy();
						while(selection.length > 0)
						{
							var cmd : A_UserCommand = new RemoveFromSelection(null, Vector, selection.pop());
							if (!firstCommand)
							{
								cmd.m_Stackable = true;
							}
							else
							{
								firstCommand = false;
							}
							A_State.s_Controller.DoUserCommand(cmd);
						}
						var cmd : A_UserCommand = new SwitchState(A_State.s_Controller.PHYSIC_SHAPE_EDITION);
						if (!firstCommand)
						{
							cmd.m_Stackable = true;
						}
						else
						{
								firstCommand = false;
						}
						A_State.s_Controller.DoUserCommand(cmd);
					}
					default :
					{
						
					}
				}
			}
			default :
			{
				
			}
		}
	}
	
	private function OnLevelEditorKeyboardEvent(_Evt:LevelEditorViewKeyboardEvent):Void
	{
		
	}
	
	private function OnLevelEditorOtherEvent(_Evt:LevelEditorViewOtherEvent):Void
	{
		switch(_Evt.m_Type)
		{
			case EDIT_RENDER_OBJECT_PHYSIC_BODY(ro) :
			{
				A_State.s_Controller.DoUserCommand(new EditPhysicShape(A_State.s_Controller.INITIAL, ro, false));
			}
			case CHANGE_PHYSIC_PROPERTY(type, value) :
			{
				A_State.s_Controller.DoUserCommand(new ChangePhysicProperty(null, type, value));
			}
			default :
			{
				
			}
		}
	}
}